<?php

namespace App\Http\Controllers\V1\BasicData;

use App\Models\V1\ApplyMajors;
use App\Models\V1\ApplyPays;
use App\Models\V1\Major;
use App\Models\V1\NewStudent;
use App\Models\V1\ReceiveStatus;
use App\Models\V1\ResultMajors;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Log;

class MajorsCollectController extends Controller
{
    /**
     * 显示志愿填报汇总数据管理页面
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('v1.collect.majors');
    }

    /**
     * 获取志愿填报汇总数据
     *
     * @param Request $request
     */
    public function postIndex(Request $request)
    {
        $response = [
            'code' => 1,
            'msg' => '',
            'count' => 0,
            'data' => [],
        ];

        // 获取符合查询条件的班级数据
        $currentPage = $request->get('page');
        $rows = $request->get('limit');
        $keywords = $request->get('keywords');

        // 获取符合查询条件的班级数据
        $data = $this->getMajorsCollectList($currentPage, $rows, $keywords);
        $formatList = array();

        if (!$data->isEmpty()) {
            $count = $data->total() > 0 ? $data->total() : 0;
            if ($count > 0) {
                // 格式化数据
                foreach ($data->items() as $item) {
                    $exam_code = $item['exam_code'];
                    $formatItem['exam_code'] = $exam_code;
                    $student = $this->getNewStudentByCode($exam_code);
                    $formatItem['student_name'] = $student['name'];
                    $formatItem['sex'] = $this->formatStudentSex($student['sex']);
                    $formatItem['enrollment_directory'] = $student['enrollment_directory'];
                    $formatItem['score'] = $student['score'];
                    //是否已报到
                    $formatItem['is_register'] = $this->getIsRegister($exam_code);
                    $formatItem['is_pay'] = $this->getIsPay($exam_code);
                    $formatItem['majors'] = $this->formatMajors($item);
                    $formatItem['majors'] = $this->getStudentMajors($exam_code);
                    $formatItem['admission_major'] = $this->getStudentAdmissionMajor($exam_code);
                    $formatItem['updated_at'] = $item['updated_at']->format('Y-m-d H:i:s');
                    $formatItem['created_at'] = $item['created_at']->format('Y-m-d H:i:s');

                    array_push($formatList, $formatItem);
                }
                $response['msg'] = '加载数据成功';
            }
        } else {
            $count = 0;
            $formatList = [];
            $response['msg'] = '加载数据成功';
        }
        $response['code'] = 0;
        $response['count'] = $count;
        $response['data'] = $formatList;
        return $response;
    }


    ///////////////////////////////////////////////////////////////////
    /// 获取数据功能函数
    ///////////////////////////////////////////////////////////////////
    /**
     * 分页查询符合条件的迎新列表数据
     *
     * @param int $page 当前页数
     * @param $rows 每页显示条数
     * @param $keywords 搜索关键词
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator 分页对象
     */
    private function getMajorsCollectList($page = 1, $rows, $keywords)
    {
        if (!isset($keywords) || empty($keywords)) {

            $data = ApplyMajors::orderBy('created_at', 'desc')
                ->paginate($rows, ['*'], 'page', $page);
        } else {
            $data = ApplyMajors::where('exam_code', 'like', '%' . $keywords . '%')
                ->orderBy('created_at', 'desc')
                ->paginate($rows, ['*'], 'page', $page);
        }
        return $data;
    }

    /***
     * 根据学生考试号获取学生详细信息
     * @param $code
     * @return mixed
     */
    private function getNewStudentByCode($code)
    {
        $student = NewStudent::where('exam_code', $code)->first();
        return $student;
    }

    /****
     * 获取学生录取专业
     * @param $code
     * @return string
     */
    private function getStudentAdmissionMajor($code)
    {
        $major = ResultMajors::where('exam_code', $code)->first();
        $result = '';
        if (!empty($major) || isset($major)) {
            $result = $major['name'];
        }
        return $result;
    }

    /****
     * 获取学生志愿信息
     * @param $code
     * @return string
     */
    private function getStudentMajors($code)
    {
        $majors = ApplyMajors::where('exam_code', $code)
            ->orderBy('wish_id', 'asc')->get();
        $result = '';
        foreach ($majors as $item) {
            $major_code = $item['major_code'];
            $major = Major::where('code', $major_code)->first();
            $name = $major['name'];
            $result = $result . $name;
        }
        return $result;
    }

    private function formatMajors($major)
    {
        $majorCode = $major['major_code'];
        $wishID = $major['wish_id'];
        $majorCodes = explode(' ', $majorCode);
        $wishIDs = explode(' ', $wishID);
        //$majorCode会根据wishIDs的排序进行排序
        array_multisort($wishIDs, $majorCodes);
        $major_name = '';
        foreach ($majorCodes as $code) {
            $major = Major::where('code', $code)->first();
            if (empty($major)) {
                $major_name = $major_name . ' ';
            } else {
                $major_name = $major_name . ' ' . $major['name'];
            }
        }
        return $major_name;
    }

    /***
     * 检测是否已报到
     * @param $code
     * @return mixed
     */
    private function getIsRegister($code)
    {

        $is_register = ReceiveStatus::where(['exam_code' => $code, 'type' => 1, 'status' => true])
            ->first();
        if (!empty($is_register) || isset($is_register)) {
            return '是';
        }
        return '否';
    }

    /***
     * 检测是否缴费
     * @param $code
     * @return mixed
     */
    private function getIsPay($code)
    {
        $pay = ApplyPays::where(['exam_code' => $code, 'status' => true])->first();
        if (!empty($pay) || isset($pay)) {
            return '是';
        }
        return '否';
    }

    /***
     * 检测是否缴费
     * @param $code
     * @return mixed
     */
    private function formatStudentSex($val)
    {
        if ($val == 1) {
            return '男';
        } elseif ($val == 2) {
            return '女';
        } else {
            return '';
        }
    }
}
